
import NativeCaller from "../NativeCaller";
import ButtonRecorder from "../ui/ButtonRecorder";
import Util from "../../../StartAb/Util";
import { ActionType } from "./Enum";

export default class ServiceLogic {

    static getXMLHttpRequest(callback = null) {
        // let url = 'http://127.0.0.1:40086'
        // let url = 'http://39.109.114.155:10086'
        let url = 'http://www.snailbaby.club:40086'
        let xhr = new XMLHttpRequest();;
        xhr.onreadystatechange = function () {
            // console.log('xhr.readyState=' + xhr.readyState + '  xhr.status=' + xhr.status);
            if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 400)) {
                // let respone = xhr.responseText;
                // let rspJson = JSON.parse(respone) || {}
                // cc.log("reponse:%s", respone)
                if (callback) {
                    callback()
                }
            }
        };
        xhr.ontimeout = function () {
            console.log("连接超时, 请稍候再试试")
            if (callback) {
                callback({ 'errorcode': 1 })
            }
        }
        xhr.onerror = function () {
            console.log("连接错误, 请检查网络：" + xhr.status)
            if (callback) {
                callback({ 'errorcode': 2 })
            }
        }
        xhr.open('POST', url, true);
        // if (cc.sys.isNative) {
        xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
        xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST');
        xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type');
        xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        // }

        // note: In Internet Explorer, the timeout property may be set only after calling the open()
        // method and before calling the send() method.
        xhr.timeout = 10000;// 8 seconds for timeout
        return xhr
    }

    static _md5Obj(body) {
        // let msg = JSON.stringify(body) + "!@#$%"
        // return md5(msg)
        return body
    }

    static _packMd5Obj(body) {
        let obj = {}
        obj['body'] = body
        // obj['md5'] = ServiceLogic._md5Obj(body)
        return obj
    }

    static register(callback) {
        // let xhr = this.getXMLHttpRequest(callback);
        // let body = {};
        // body['cmd'] = 'register'
        // body['uuid'] = Util.getOrGenerateUUID()
        // body['lang'] = cc.sys.language
        // body['langCode'] = cc.sys.languageCode
        // body['version'] = NativeCaller.getVersionName()
        // body['plat'] = "XM"
        // xhr.send(JSON.stringify(ServiceLogic._packMd5Obj(body)))
    }

    //登陆(打开游戏)
    static login() {
        this.action(ActionType.ActionLogin, {})
    }

    static action(actionType, info) {
        // let xhr = this.getXMLHttpRequest();
        // let body = info;
        // body['cmd'] = 'action'
        // body['uuid'] = Util.getOrGenerateUUID()
        // body['version'] = NativeCaller.getVersionName()
        // body.action = actionType
        // xhr.send(JSON.stringify(ServiceLogic._packMd5Obj(body)))
    }
    //进入关卡
    //结束关卡(leave)

    static submitErrorLog(stack: string) {
        // if (!stack || stack.length == 0)
        //     return
        // stack = stack.substring(0, 512)
        // let xhr = this.getXMLHttpRequest();
        // let body = {};
        // body['cmd'] = 'log'
        // body['log'] = "uuid:" + Util.getOrGenerateUUID() + " scene:" + cc.director.getScene().name + " ver:" + NativeCaller.getVersionName()
        //     + ButtonRecorder.getButtonRecordString() + "\n"
        //     + stack + "\n"
        // xhr.send(JSON.stringify(ServiceLogic._packMd5Obj(body)))
    }
}
